/**
 * @(#)ApkConfigController.java 2017年03月1日
 * Copyright(C) 2017 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jsbd.boss.common.ResponseUtils;
import org.jsbd.boss.domian.AppLogAction;
import org.jsbd.boss.mq.MqLogManager;
import org.jsbd.boss.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


@Controller
@RequestMapping("/app/log")
public class AppLogController {

	public static final Logger LOGGER = LoggerFactory.getLogger(AppLogController.class);

	@RequestMapping(value = "action", method = RequestMethod.POST)
	@ResponseBody
	public void action(String mark, String imsi, String imei, String taskType, String orderId, String data,
			HttpServletResponse response) throws Exception {
		try {
			JSONArray list = JSON.parseArray(data);
			int length = list.size();
			String date = DateUtil.getCurrentDateStr();

			for (int i = 0; i < length; i++) {
				AppLogAction appLogAction = new AppLogAction();
				appLogAction.setMark(mark);
				appLogAction.setImsi(imsi);
				appLogAction.setImei(imei);
				appLogAction.setTaskType(taskType);
				appLogAction.setOrderId(orderId);

				JSONObject object = list.getJSONObject(i);
				appLogAction.setActionName(object.getString("actionName"));
				appLogAction.setActionEvent(object.getString("actionEvent"));
				appLogAction.setActionContent(object.getString("actionContent"));
				appLogAction.setActionResult(object.getString("actionResult"));
				appLogAction.setRemark(object.getString("remark"));
				appLogAction.setExt(object.getString("ext"));
				appLogAction.setCurrentDate(date);
				MqLogManager.saveAppActionLog(appLogAction);
			}
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			LOGGER.error("AppLogError", e);
			LOGGER.error("mark="+mark+",AppLogError_data="+data);
			ResponseUtils.responseFailure(response);
		}
	}
	
	@RequestMapping(value = "test")
	@ResponseBody
	public void test(HttpServletRequest request, HttpServletResponse response) throws Exception {

		 AppLogAction appLogAction = new AppLogAction();
		 appLogAction.setMark("d");
		 appLogAction.setImsi("d1");
		 appLogAction.setImei("d2");
		 appLogAction.setTaskType("q");
		 appLogAction.setOrderId("d3");
		 appLogAction.setActionName("d4");
		 appLogAction.setActionEvent("d5");
		 appLogAction.setActionContent("d6");
		 appLogAction.setActionResult("d7");
		 appLogAction.setRemark("d8");
		 appLogAction.setExt("d9");
		 appLogAction.setCurrentDate(DateUtil.getCurrentDateStr());
		 MqLogManager.saveAppActionLog(appLogAction);
	}
}
